package demo.zyy.com.dreamstudio.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/8/14.
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    Unbinder unbinder;
    protected final String TAG = this.getClass().getSimpleName();
    private View mContextView = null;
    long lastClick = 0;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContextView=inflater.inflate(loadXML(),container,false);
        unbinder = ButterKnife.bind(this, mContextView);
        initView(mContextView);
        initData();
        initListener();
        return mContextView;
    }

    //绑定布局
    protected abstract int loadXML();
    //初始化控件
    protected abstract void initView(View view);
    //初始化数据
    protected abstract void initData();

    protected abstract void initListener();
    /** View点击 **/
    public abstract void widgetClick(View v);

    @Override
    public void onClick(View v) {
        if (fastClick())
            widgetClick(v);
    }
    /**
     * [防止快速点击]
     *
     * @return
     */
    private boolean fastClick() {
        if (System.currentTimeMillis() - lastClick <= 1000) {
            return false;
        }
        lastClick = System.currentTimeMillis();
        return true;
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
